import { JSONMapping } from "../../json-mapping";
import {SyncStatus} from "../../sync-status";
import {Attachment} from "../attachment";


export class InspectionOperationsTickets extends JSONMapping {
    static PropertyKeyToJSONKeyMap() {
        return {
            taskId: "taskId|planId",
            type: "operateType",
            fromCompany: "orderDept",
            fromName: "orderMan",
            toName: "orderFromMan",
            orderTakenTime: "orderFromTime",
            operationBeginTime: "operateStartTime",
            operationEndTime: "operateEndTime",
            operatorName: "operator",
            guarder: "guardian",
            dutyStackholder: "dutyLeader",
            attachmentIds: "pictureIds",
        };
    }


    public id: string = null;                       // 操作票Id， 编号， 索引列
    public taskId: string = null;                   // 检修任务的Id
    public type: string = null;                     // 类型
    public fromCompany: string = null;              //  发令单位
    public fromName: string  = null;                //  发令人
    public fromId: string  = null;                  //  发令人Id
    public toName: string = null;                   //  受令人
    public toId: string  = null;                    //  受令人Id
    public orderTakenTime: number = null;           //  受令时间
    public operationBeginTime: number = null;       //  操作开始时间
    public operationEndTime: number = null;         //  操作结束时间
    public operatorName: string = null;             //  操作人
    public operatorId: string = null;               //  操作人Id
    public guarder: string = null;                  //  监护人
    public guarderId: string = null;                // 监护人Id
    public dutyStackholder: string = null;          //  值班负责人
    public dutyStackholderId: string = null;        //  值班负责人id
    public remark: string = null;                   // 备注
    public attachmentIds: string = null;            //  拍照附件的的Id列表，逗号分隔
    public pictureAttachments: Array<Attachment> = [];
    public syncStatus: SyncStatus = SyncStatus.None;               // 同步状态：   （0: 从来没上传, 1: 上传成功, 2: 正在上传, 3: 上传失败)


    constructor(json: { [key: string]: any }) {
        super(json);
        InspectionOperationsTickets.mapFromJSON(json, this);
    }

    updateAttachmentIdWithNewId(oldAttachmentId: string, newAttachmentId: string) {
      let attachment: Attachment = null;
      if (this.pictureAttachments.some((pictureAttachment, index) => {
        let result = pictureAttachment.id === oldAttachmentId;
        if (result) {
          attachment = pictureAttachment;
        }
        return result;
      })) {
        attachment.id = newAttachmentId;

      }
    }

    addAttachments(paths: Array<string>): Array<Attachment> {
        let attachments = paths.map((path, index) => {
          let id = new Date().getTime() + '' + (Math.ceil(Math.random() * 1000)) + '' + index;
          let name = path.split('/');
          return new Attachment({id: id , attachmentName: name[name.length - 1], path: path, syncStatus: SyncStatus.None});
        });
        return attachments;
    }
}
